package com.feng.oauth2;

import com.alibaba.druid.pool.DruidDataSource;
import com.feng.oauth2.oauth.domain.oauth.CustomJdbcClientDetailsService;
import com.feng.oauth2.oauth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.vote.AffirmativeBased;
import org.springframework.security.access.vote.AuthenticatedVoter;
import org.springframework.security.access.vote.RoleVoter;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@EnableWebSecurity(debug = true)
@Order(2)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DruidDataSource dataSource;
    @Autowired
    @Qualifier("ouathUserService")
    private UserService ouathUserService;

    @Autowired
    private CustomJdbcClientDetailsService customJdbcClientDetailsService;
    @Autowired
    private DefaultTokenServices tokenService;


    @Override
    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().
//                antMatchers("/oauth/token").
//                antMatchers("/oauth/authorize")
//        .antMatchers("test/test");
//        web.ignoring().antMatchers("/login");
        //web.ignoring().antMatchers("/oauth/**");
        web.ignoring().antMatchers("/**/favicon.ico");
        web.debug(true);// web.debug(true);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginProcessingUrl("/api/login").passwordParameter("password")
                .usernameParameter("username");
        http.sessionManagement().enableSessionUrlRewriting(false)

                .and().authorizeRequests().antMatchers("/oauth/**","/api/login").permitAll().and()
        .csrf().disable();

//                .and().formLogin().and().anonymous().disable().csrf().disable();
//        http.sessionManagement().enableSessionUrlRewriting(false).and().authorizeRequests().anyRequest().permitAll();


    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.userDetailsService(ouathUserService).passwordEncoder(new BCryptPasswordEncoder());

    }
    @Bean(name = "authenticationManagerBean")
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
//        ProviderManager providerManager = new ProviderManager();
        return super.authenticationManagerBean();
//        OAuth2AuthenticationManager oAuth2AuthenticationManager = new OAuth2AuthenticationManager();
//        oAuth2AuthenticationManager.setClientDetailsService(customJdbcClientDetailsService);
//        oAuth2AuthenticationManager.setTokenServices(tokenService);
//        return oAuth2AuthenticationManager;
    }


}
